package com.gs.netty.Quickstart.controller;

import com.gs.netty.client.Message;
import com.gs.netty.client.SendCallback;
import com.gs.netty.client.SendResult;
import com.gs.netty.client.exception.*;
import com.gs.netty.client.remoting.WebSocketClient;
import com.gs.netty.common.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    private WebSocketClient webSocketClient;

    @Autowired
    private TestService testService;



    @GetMapping("/test")
    public void test(@RequestParam String cmd) throws RuntimeException {


        Message message = new Message();
        message.setCommand(cmd);
        message.setBody("你好呀");
        try {
            SendResult send = webSocketClient.send(message);

            System.out.println("send = " + send);
        } catch (WebSocketClientHandshakerException e) {
            throw new RuntimeException(e);
        } catch (WebSocketRemotingConnectException e) {
            throw new RuntimeException(e);
        } catch (WebSocketNotFountChannelException e) {
            throw new RuntimeException(e);
        } catch (WebSocketTooMuchRequestTimeoutException e) {
            throw new RuntimeException(e);
        } catch (WebSocketClientException e) {
            throw new RuntimeException(e);
        }

    }
}
